package com.twotiger.and.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.bean.Bank;
import com.twotiger.and.view.PickerView.onSelectListener;

public class OptionPopupWindow2 extends PopupWindow implements OnClickListener {

    private View rootView; // 总的布局
    private View btnSubmit, btnCancel;
    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";

    private Context mContext;
    private TextView mTextView;
    private PickerView minute_pv;

    private ArrayList<String> mData;
    private String mCurrentText;
    private int mCurrentSelected;
    private List<Bank> mBankList;

    public OptionPopupWindow2(Context context, TextView textView, List<Bank> bankList) {
        super(context);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.mContext = context;
        this.mTextView = textView;
        this.mBankList = bankList;

        prepareData(bankList);

        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new BitmapDrawable());// 这样设置才能点击屏幕外dismiss窗口
        this.setOutsideTouchable(true);

        mCurrentSelected = mData.size() / 2;
        //      mCurrentText = (String) mData.get(mCurrentSelected).subSequence(0, 4);
        mCurrentText = (String) mData.get(mCurrentSelected);
        mTextView.setText(mCurrentText);

        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        rootView = mLayoutInflater.inflate(R.layout.layout_pop, null);
        // -----确定和取消按钮
        btnSubmit = rootView.findViewById(R.id.btnSubmit);
        btnCancel = rootView.findViewById(R.id.btnCancel);
        btnSubmit.setTag(TAG_SUBMIT);
        btnCancel.setTag(TAG_CANCEL);
        minute_pv = (PickerView) rootView.findViewById(R.id.minute_pv);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        initData();

        setContentView(rootView);
    }

    private void prepareData(List<Bank> bankList) {

        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < bankList.size(); i++) {
            Bank bank = bankList.get(i);
            list.add(bank.getName() + " " + bank.getPerAmt() + "万/笔" + bank.getDayAmt() + "万/日");
        }
        this.mData = list;
    }

    private void initData() {
        minute_pv.setData(mData);
        minute_pv.setOnSelectListener(new onSelectListener() {

            @Override
            public void onSelect(String text, int currentSelected) {
                //                mTextView.setText(text.subSequence(0, 4));
                mTextView.setText(text);
                mCurrentSelected = currentSelected;
                mCurrentText = text;
            }
        });
    }

    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        if (tag.equals(TAG_CANCEL)) {
            dismiss();
            mTextView.setText("");
            return;
        } else {
            //          mTextView.setText(mCurrentText.subSequence(0, 4));
            mTextView.setText(mCurrentText);
            dismiss();
            return;
        }
    }

    /**
     * 获取位置
     * 
     * @return
     */
    public int getselectpositon() {
        int position = 0;
        for (int i = 0; i < mBankList.size(); i++) {
            Bank bank = mBankList.get(i);
            if ((bank.getName() + " " + bank.getPerAmt() + "万/笔" + bank.getDayAmt() + "万/日").equals(mData
                .get(mCurrentSelected))) {
                position = i;
                break;
            }
        }
        return position;
    }

}
